package com.zzyweb.rentalcarsys.controller;

import com.zzyweb.rentalcarsys.entity.News;
import com.zzyweb.rentalcarsys.service.NewsService;
import com.zzyweb.rentalcarsys.utils.PageResult;
import com.zzyweb.rentalcarsys.utils.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhiyi
 * @date 10/1/25
 **/
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {
    @Autowired
    NewsService newsService;
    @GetMapping("/all")
    public ResponseObject<PageResult> getAll(@RequestParam Long currentPage,@RequestParam Long pageSize) {
        PageResult<News> allNews = newsService.getAllNews(currentPage, pageSize, null);
        return ResponseObject.ok(allNews);
    }
    @PostMapping("/conditions")
    public ResponseObject<PageResult> getAllByConditions(@RequestParam Long currentPage, @RequestParam Long pageSize,@RequestBody News news) {
        PageResult<News> allNews = newsService.getAllNews(currentPage, pageSize, news);
        return ResponseObject.ok(allNews);
    }

    @PostMapping("/addNews")
    public ResponseObject addNews(@RequestBody News news) {
        Long newsId = newsService.addNews(news);
        return ResponseObject.ok();
    }

    @DeleteMapping("/deleteOne/{newsId}")
    public ResponseObject deleteNews(@PathVariable String newsId) {
        newsService.deleteNewsById(newsId);
        return ResponseObject.ok();
    }

    @DeleteMapping("/deleteBatch/{newsIds}")
    public ResponseObject deleteNewsBatch(@PathVariable String newsIds) {
        newsService.deleteNewsBatch(newsIds);
        return ResponseObject.ok();
    }
    @PutMapping("/modify")
    public ResponseObject modifyNews(@RequestBody News news) {
        newsService.modifyNews(news);
        return ResponseObject.ok();
    }
}
